
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <QMessageBox>
#include <QMimeData>
#include <QDragEnterEvent>
#include <QDropEvent> 

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setAcceptDrops(true); // 允许窗口接受拖放事件
}
MainWindow::~MainWindow()
{

}

void MainWindow::dragEnterEvent(QDragEnterEvent *event) {
    if (event->mimeData()->hasUrls()) {
        event->acceptProposedAction();
    }
}

void MainWindow::dropEvent(QDropEvent *event) {
    const QMimeData *mimeData = event->mimeData();
    if (mimeData->hasUrls()) {
        QList<QUrl> urlList = mimeData->urls();
        for (int i = 0; i < urlList.size() && i < 32; ++i) {
            QString fileName = urlList.at(i).toLocalFile();
            if (!fileName.isEmpty()) {
                processFile(fileName);
            }
        }
    }
}

void MainWindow::processFile(const QString &fileName) {
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, "Modify File", "Do you want to modify this file?",
                                  QMessageBox::Yes|QMessageBox::No);
    if (reply == QMessageBox::Yes) {
        QFile file(fileName);
        if (file.open(QIODevice::Append)) {
            char zeroByte = 0;
            file.write(&zeroByte, 1);
            file.close();
            QMessageBox::information(this, "Success", "File modified successfully!");
        } else {
            QMessageBox::critical(this, "Error", "Failed to open file!");
        }
    }
}


